﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using System.Collections.Generic;

namespace HZ.Supervise.Web.Secondphase.ReportHospital
{
	public partial class hospitalAmountSubsidyDetails : BasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
				SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
				string strSubsidyId = sq.SubsidyID;
				if (!string.IsNullOrEmpty(strSubsidyId)) //如果月份为空 参数有误
				{
					BindArea();
					banding();
				}
				else
				{
					Response.Write("<script>history.go(-1);<script>");
				}
				
			}
		}
		public void BindArea()
		{
			IBLL_Area ia = new HZ.Supervise.BLL.BLL_Area();
			IList<Area> lstArea = new List<Area>();
			string userAreaId = CurrentAccount.GetUser().Org.Area.AreaID;
			//判断用户所属地区，如果用户是省级的 则可以列出全部区域
			//if (userAreaId == CurrentAccount.GetCurrentDeployArea().AreaID)
			//{
			int i;
			lstArea = ia.GetSonAreas(userAreaId, 1, 0, out i);
			foreach (Area area in lstArea)
			{
				ListItem ddlitem = new ListItem();
				ddlitem.Text = area.AreaName;
				ddlitem.Value = area.AreaID;
				ddlCity.Items.Add(ddlitem);
			}

			ListItem b = new ListItem();
			b.Text = "全部";
			b.Value = "";
			ddlCity.Items.Insert(0, b);


		}
		//绑定未补助的医院
		private void banding()
		{
			int totalrecord = 0;
			HZ.Supervise.IBLL.IBLL_Prescription iPrescription = HZ.Supervise.BLLFactory.IBl_Prescription();
			string strwhere = " ";
			string strHostpitalid = "";
			ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
			SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
			strHostpitalid = sq.SubsidyID;
			string strArea = ddlCity.SelectedValue;
			if (strArea.Equals(""))//如果选全部
			{
				if (CurrentAccount.GetUser().Org.Area.AreaID.Substring(4, 2).Equals("00")) //如果是市级单位可以查看该市下属地方
				{
					strwhere += " and Sareaid like '" + CurrentAccount.GetUser().Org.Area.AreaID.Substring(0, 4) + "%'";
				}
				else
				{
					strwhere += " and Sareaid = '" + CurrentAccount.GetUser().Org.Area.AreaID + "'";
				}
			}
			else
			{
				strwhere += " and Sareaid = '" + strArea + "'";
			}
			if (!string.IsNullOrEmpty(strHostpitalid))
			{
				strwhere += "and SHospitalName='" + strHostpitalid + "'";
			}
			if (!txtStratTime.Text.Trim().Equals("")) //开始时间
			{ strwhere += " and Saddtime >='" + (new HZ.Common.ConvertHelper(txtStratTime.Text.Trim()).ToDateTime).Value.ToString() + "'"; }
			if (!txtEndTime.Text.Trim().Equals("")) //结束时间
			{ strwhere += " and Saddtime <='" + (new HZ.Common.ConvertHelper(txtEndTime.Text.Trim()).ToDateTime).Value.ToString() + "'"; }
			DataTable dt = iPrescription.GetSubsidy(strwhere, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);
			AspNetPager1.RecordCount = totalrecord;
			this.gvwDetail.DataSource = dt;
			this.gvwDetail.DataBind();


		}

		protected void btnQuery_Click(object sender, EventArgs e)
		{
            AspNetPager1.CurrentPageIndex = 1;
			banding();
		}

		protected void AspNetPager1_PageChanged(object sender, EventArgs e)
		{
			banding();
		}

		protected void gvwDetail_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName == "SHospitalName")
			{
				string sessionid = Guid.NewGuid().ToString();
				SuperviseQuery query = (SuperviseQuery)ViewState["query"];
				try
				{
					query.SubsidyID = e.CommandArgument.ToString();
				}
				catch { }
				Session[sessionid] = query;
				string url = "HospitalAmountSubsidyDetail.aspx";
				Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
			}
		}
	}
}
